package hamster.csustef.acquisition.api;

import cn.hutool.core.io.FileUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ElectricApi {

    private static final String dormKeysFilePath = "/root/IdeaProjects/csust_electric_fee_dw/resources/dormKeys/dormKeys.csv";
    private static List<String> dormKeysList = null;

    public static List<String> getDormKeysList() {
        if (dormKeysList == null) dormKeysList = FileUtil.readUtf8Lines(dormKeysFilePath);
        return dormKeysList;
    }


    public static String getFee(String id) {
        String[] split = id.split(":");
        String buildingId = split[0];
        String roomId = split[1];
        String u = "http://yktwd.csust.edu.cn:8988/web/Common/Tsm.html";
        Map<String, Object> map = new HashMap<>();
        map.put("json", "true");
        map.put("funname", "synjones.onecard.query.elec.roominfo");
        map.put("jsondata", "{\"query_elec_roominfo\": {" +
                "\"aid\": \"0030000000002501\"," +
                " \"account\": \"000001\"," +
                " \"room\": {\"roomid\": \"" + roomId + "\", \"room\": \"\"}," +
                " \"floor\": {\"floorid\": \"\", \"floor\": \"\"}," +
                " \"area\": {\"area\": \"云塘校区\", \"areaname\": \"云塘校区\"}," +
                " \"building\": {\"buildingid\": \"" + buildingId + "\", \"building\": \"\"}}}");
        String result = HttpUtil.post(u, map);
        try {
            JSONObject jsonObject = JSON.parseObject(result);
            String retcode = jsonObject.getJSONObject("query_elec_roominfo").getString("retcode");
            String msg = jsonObject.getJSONObject("query_elec_roominfo").getString("errmsg");
            if ("0".equals(retcode)) {
                return msg.substring(7);
            } else {
                return "ERROR:" + msg;
            }
        } catch (Exception e) {
            return "ERROR:" + result;
        }
    }

    public static void main(String[] args) {
        getDormKeysList();
        long time = new Date().getTime();
        System.out.println(time % (1000 * 60 * 30) / 1000);
        System.out.println(getFee("71:A415"));
        System.out.println(new Date().getTime() - time);
    }

}
